package com.qd.panda.api;

import com.cdqidi.annotation.JumpResponseAnnotation;
import com.cdqidi.util.JacksonUtil;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.qd.panda.service.AsyncService;
import com.qd.panda.service.third.coupon.CouponService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 成都停车平台回调消费券核销接口
 *
 * @author sjk
 * @create 2023-12-14 14:37
 * @since 1.0.0
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/api/v1/")
@Slf4j
public class ThirdApiController {
    private final AsyncService asyncService;
    private final CouponService couponService;

    /**
     * 优惠券核销接口
     *
     * @param couponCode 优惠券券码
     * @param sign       签名
     * @return 记录
     */
    @JumpResponseAnnotation
    @GetMapping(value = "/coupon/couponUsed")
    public ObjectNode getById(String couponCode, String sign) {
        ObjectNode objectNode = JacksonUtil.getObjectMapper().createObjectNode();
        objectNode.put("code", 1);
        if (!StringUtils.hasLength(couponCode)) {
            objectNode.put("message", "优惠券券码不能为空");
            return objectNode;
        }
        if (!StringUtils.hasLength(sign)) {
            objectNode.put("message", "签名不能为空");
            return objectNode;
        }
        boolean isSign = couponService.isSign(couponCode, sign);
        log.info("couponCode: {},sign: {},isSign: {}", couponCode, sign, isSign);
//        if (isSign) {
        asyncService.couponUsed(couponCode);
        objectNode.put("code", 0);
        objectNode.put("message", "操作成功");
//        } else {
//            objectNode.put("message", "签名错误");
//        }
        return objectNode;
    }
}